#include "AppMenu.h"

#include <widgets/SImageButton.h>

#include <QApplication>
#include <QBoxLayout>
#include <QLabel>
#include <QLineEdit>
#include <QMouseEvent>
#include <QPropertyAnimation>

#include "AppMenuPanel.h"
#include "core/StyleMgr.h"

AppMenu::AppMenu(MainFrame* frame, QWidget* parent)
    : QWidget(parent), m_frame(frame) {
  m_btnLogo = new SImageButton(this, "guiframe_app_logo_title");
  m_btnLogo->setAttribute(Qt::WA_TransparentForMouseEvents, true);
  m_btnLogo->setTopOffset(-6);

  QHBoxLayout* layout = new QHBoxLayout();
  layout->addWidget(m_btnLogo);
  layout->setSizeConstraint(QLayout::SizeConstraint::SetFixedSize);
  setLayout(layout);

  m_appMenu = new AppMenuPanel(this);
  m_appMenu->setVisible(false);
  connect(this, &AppMenu::setCommonPage, m_appMenu,
          &AppMenuPanel::onCommonPage);
}

AppMenu::~AppMenu() {}

bool AppMenu::eventFilter(QObject* watched, QEvent* e) {
  if (e->type() == QEvent::MouseMove) {
    if (m_appMenu->isVisible()) {
      QRect myRect(mapToGlobal(rect().topLeft()),
                   QSize(rect().width(), rect().height()));
      QRect menuPanelRect(m_appMenu->mapToGlobal(QPoint(0, 0)),
                          m_appMenu->size());
      QPoint pos = QCursor::pos();
      if (!myRect.contains(pos) && !menuPanelRect.contains(pos)) {
        m_timerHideMenu = startTimer(500);
      }
    } else {
      KillHideMenuTimer();
    }
  }
  return QWidget::eventFilter(watched, e);
}

void AppMenu::enterEvent(QEvent* event) {
  if (!m_appMenu->isVisible()) {
    KillShowMenuTimer();
    m_timerShowMenu = startTimer(1000);
  }
}

void AppMenu::leaveEvent(QEvent* event) { KillShowMenuTimer(); }

void AppMenu::mousePressEvent(QMouseEvent* event) {
  QWidget::mousePressEvent(event);
}

void AppMenu::mouseReleaseEvent(QMouseEvent* event) {
  if (!m_appMenu->isVisible()) {
    KillShowMenuTimer();
    onShowMenu();
  }
  QWidget::mouseReleaseEvent(event);
}

void AppMenu::timerEvent(QTimerEvent* e) {
  if (e->timerId() == m_timerShowMenu) {
    KillShowMenuTimer();
    onShowMenu();
  } else if (e->timerId() == m_timerHideMenu) {
    KillHideMenuTimer();
    onHideMenu();
  }
}

void AppMenu::onShowMenu() {
  if (!m_appMenu->isVisible()) {
    QRect myRect(mapToGlobal(rect().topLeft()),
                 QSize(rect().width(), rect().height()));
    QRect menuPanelRect(m_appMenu->mapToGlobal(QPoint(0, 0)),
                        m_appMenu->size());
    QPoint pos = QCursor::pos();
    if (myRect.contains(pos) || menuPanelRect.contains(pos)) {
      m_appMenu->move(mapToGlobal(rect().topLeft()));
      m_appMenu->show();
      qApp->installEventFilter(this);
    }
  }
}

void AppMenu::onHideMenu() {
  if (m_appMenu->isVisible()) {
    m_appMenu->hide();
  }
  qApp->removeEventFilter(this);
}

void AppMenu::KillShowMenuTimer() {
  if (m_timerShowMenu != -1) {
    killTimer(m_timerShowMenu);
    m_timerShowMenu = -1;
  }
}

void AppMenu::KillHideMenuTimer() {
  if (m_timerHideMenu != -1) {
    killTimer(m_timerHideMenu);
    m_timerHideMenu = -1;
  }
}

void AppMenu::onCommonPage(int* ids, int size, int ntype) {
  setCommonPage(ids, size, ntype);
}
